package com.hongyuan.sellfitness.ui.find.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.rxbus.SearchFindMessage;
import com.hongyuan.sellfitness.ui.find.beans.FindChildBean;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FindSearchPostViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public FindSearchPostViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<FindChildItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FindChildItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_find_child);

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(SearchFindMessage.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(message -> {
                    observableList.clear();

                    //更新下数据
                    setParams("search_name",message.getSearchContent());
                    requestData(Constants.FIND_CIRCLE_LIST);
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.FIND_CIRCLE_LIST){
            List<FindChildBean.ListBean> mList = ((FindChildBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(FindChildBean.ListBean bean : mList){
                    FindChildItemViewModel itemViewModel = new FindChildItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.ADD_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(1);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() + 1);
                itemViewModel.entity.notifyChange();
            }
        }

        if(code == Constants.CANCEL_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(0);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() - 1);
                itemViewModel.entity.notifyChange();
            }
        }
    }

    //操作的子项
    private FindChildItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);

        itemViewModel = (FindChildItemViewModel) itemVM;

    }
}
